﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;
using MMS.DAO;
using System.Windows.Forms;
namespace MMS
{
    public static class ValidateUtil
    {
        public static string reg = "";
        public static UnitDAO unitDAO = new UnitDAO(Param.ConnectionString.Value);
        public static string removeCommaFromString(string str)
        {
            return str.Replace(",", "");
        }
        public static Boolean isValidEmail(string str)
        {

            reg = @"((([\w]+\.[\w]+)+)|([\w]+))@(([\w]+\.)+)([A-Za-z]{1,3})";

            return validateByRegx(str, reg);

        }

        private static Boolean validateByRegx(string str, string reg)
        {
            Boolean result = false;
            MatchCollection myMatchCollection = Regex.Matches(str, reg);
            foreach (Match myMatch in myMatchCollection)
            {
                if (myMatch.Value.Equals(str))
                {
                    result = true;
                }

            }
            return result;
        }

        public static Boolean isValidNum(string str)
        {
            //neu co @, chi can ghi \d, neu khong phai ghi \\d
            reg = @"\d+";

            return validateByRegx(str, reg);
        }
        public static Boolean isValidTextOnly_Method2(string str)
        {
            bool result = false;
            //kiem tra chuoi dua vao chi la ky tu \w. Neu la chuoi chi chua cac ky tu thi xu ly tiep
            reg = @"\w+";
            if (validateByRegx(str, reg))
            {
                //neu chuoi dua vao ma co chua ky tu so thi return false
                reg = @"[0-9]+";
                if (validateByRegx(str, reg))
                {
                    result = false;
                }
                else
                {
                    result = true;
                }
            }


            return result;
        }

        public static Boolean isValidTextOnly(string str)
        {
            string reg = @"[a-zA-Z_]{1,20}";
            return validateByRegx(str, reg);

        }

        public static Boolean isValidTextAndNum(string str)
        {
            reg = @"[a-zA-Z_0-9]+";

            return validateByRegx(str, reg);
        }

        public static Boolean isValidCustomerDTO(CustomerDTO customerDTO)
        {
            Boolean result = false;
            result = isValidEmail(customerDTO.Email);
            result = isValidNum(customerDTO.Phone1);
            result = isValidNum(customerDTO.Phone2);
            result = isValidTextOnly(customerDTO.Name);
            return result;

        }
        public static Boolean isValidUserDTO(UserDTO userDTO)
        {
            Boolean result = false;
            result = isValidTextAndNum(userDTO.UserName);

            result = isValidEmail(userDTO.Email);

            result = isValidTextOnly(userDTO.Name);
            return result;

        }
        public static bool canDelete(UnitDTO u)
        {
            bool result = true;
            if (unitDAO.isIdUsing((u.Id)))
            {
                result = false;
            }
            return result;

        }
        internal static string formatNum(string str)
        {
            string strpriceByDay = "";
            if (!str.Equals(""))
            {

                string priceByDay = ValidateUtil.removeCommaFromString(str);
                double valuepriceByDay = double.Parse(priceByDay);
                if (valuepriceByDay > 9)
                {
                    strpriceByDay = valuepriceByDay.ToString("0,0", CultureInfo.InvariantCulture);
                }
                else
                {
                    strpriceByDay = valuepriceByDay.ToString();
                }
            }
            return strpriceByDay;
        }
        internal static string formatVNCurrency(string str)
        {
            string strVNCurrency = "";
            if (!str.Equals(""))
            {
                double valuepriceByDay = double.Parse(str);
                if (valuepriceByDay > 9)
                {
                    strVNCurrency = valuepriceByDay.ToString("0,0", CultureInfo.InvariantCulture);
                }
                else
                {
                    strVNCurrency = valuepriceByDay.ToString();
                }
            }
            return strVNCurrency + "VNĐ";
        }
        public static void isLetterAndSpace(KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Space)
            {
                e.Handled = false;
            }
            else
            {
                if (!Char.IsLetter(e.KeyChar) && !Char.IsControl(e.KeyChar))
                {
                    e.Handled = true;
                }
            }
        }
        public static void isDigit(KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }
        public static void IsLetterOrDigit(KeyPressEventArgs e)
        {
            if (!Char.IsLetterOrDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }
        public static string ConvertToUnSign(string text)
        {

            for (int i = 33; i < 48; i++)
            {

                text = text.Replace(((char)i).ToString(), "");

            }



            for (int i = 58; i < 65; i++)
            {

                text = text.Replace(((char)i).ToString(), "");

            }

            for (int i = 91; i < 97; i++)
            {

                text = text.Replace(((char)i).ToString(), "");

            }

            for (int i = 123; i < 127; i++)
            {

                text = text.Replace(((char)i).ToString(), "");

            }

            text = text.Replace(" ", "-");

            Regex regex = new Regex(@"\p{IsCombiningDiacriticalMarks}+");

            string strFormD = text.Normalize(System.Text.NormalizationForm.FormD);

            return regex.Replace(strFormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');

        }
    }
}
